home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / dskut / fips11.zip / SOURCE / MAIN.CPP < prev    next >
C/C++ Source or Header  |  1994-05-25  |  6KB  |  209 lines

  1. /*
  2.     FIPS - the First nondestructive Interactive Partition Splitting program
  3.  
  4.     Module main.cpp
  5.  
  6.     RCS - Header:
  7.     $Header: c:/daten/fips/source/main/RCS/main.cpp 1.1 1994/05/25 22:20:03 schaefer Exp schaefer $
  8.  
  9.     Copyright (C) 1993 Arno Schaefer
  10.  
  11.     This program is free software; you can redistribute it and/or modify
  12.     it under the terms of the GNU General Public License as published by
  13.     the Free Software Foundation; either version 2 of the License, or
  14.     (at your option) any later version.
  15.  
  16.     This program is distributed in the hope that it will be useful,
  17.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.     GNU General Public License for more details.
  20.  
  21.     You should have received a copy of the GNU General Public License
  22.     along with this program; if not, write to the Free Software
  23.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25.  
  26.     Report problems and direct all questions to:
  27.  
  28.     schaefer@rbg.informatik.th-darmstadt.de
  29. */
  30.  
  31. #include <stdlib.h>
  32. #include "logdr_st.h"
  33. #include "global.h"
  34. #include "input.h"
  35. #include "fat.h"
  36. #include "fipsspec.h"
  37. #include "host_os.h"
  38.  
  39.  
  40. void main (int argc,char *argv[])
  41. {
  42.     evaluate_argument_vector (argc,argv);
  43.  
  44.     atexit (exit_function);
  45.  
  46.     if (global.debug_mode) global.open_debugfile(argc,argv);
  47.  
  48.     notice();
  49.  
  50.     int drive_number;
  51.     if (global.drive_number_cmdline) drive_number = global.drive_number_cmdline;
  52.     else drive_number = ask_for_drive_number ();
  53.  
  54.     host_os os;
  55.     char infostring[256];
  56.  
  57.     if (os.ok () != OK)
  58.     {
  59.         printx ("\nWARNING: FIPS has detected that it is running under %s\n"
  60.             "FIPS should not be used under a multitasking OS. If possible, boot from a DOS\n"
  61.             "disk and then run FIPS. Read FIPS.DOC for more information.\n\n",
  62.             os.information (infostring));
  63.         ask_if_proceed ();
  64.     }
  65.  
  66.     fips_harddrive harddrive (drive_number);
  67.  
  68.     if (harddrive.errorcode)
  69.         error ("Error reading Drive Geometry: Errorcode %u",harddrive.errorcode);
  70.  
  71.     harddrive.reset();
  72.  
  73.     if (harddrive.errorcode)
  74.         error ("Drive Initialization Failure: Errorcode %u",harddrive.errorcode);
  75.  
  76.     if (harddrive.rootsector->read ())
  77.         error ("Error reading Root Sector");
  78.  
  79.     if (global.debug_mode)
  80.     {
  81.         fprintf (global.debugfile,"\nRoot Sector Drive %02Xh:\n\n",drive_number);
  82.         hexwrite (harddrive.rootsector->data,512,global.debugfile);
  83.     }
  84.  
  85.     fips_partition *partition;
  86.  
  87.     while (true)
  88.     {
  89.         fips_harddrive hd = harddrive;
  90.  
  91.         hd.partition_table().get (hd.rootsector);
  92.  
  93.         printx ("\nPartition Table:\n\n");
  94.         hd.print_partition_table ();
  95.  
  96.         hd.check();
  97.  
  98.         int partition_number;
  99.         if (global.partition_number_cmdline) partition_number = global.partition_number_cmdline - 1;
  100.         else partition_number = ask_for_partition_number (hd.partition_table().partition_info);
  101.  
  102.         partition = new fips_partition (&hd,partition_number);
  103.  
  104.         int system = partition->partition_info->system;
  105.         if (system == 5)
  106.             error ("Can't split extended Partitions");
  107.         if (system == 0)
  108.             error ("Invalid Partition selected: %u",partition_number + 1);
  109.         if ((system != 1) && (system != 4) && (system != 6))
  110.             error ("Unknown Filesystem: %02Xh",system);
  111.  
  112.         if (partition->bootsector->read ())
  113.             error ("Error reading Boot Sector");
  114.  
  115.         if (global.debug_mode)
  116.         {
  117.             fprintf (global.debugfile,"\nBoot Sector Drive %02Xh, Partition %u:\n\n",hd.number,partition->number + 1);
  118.             hexwrite (partition->bootsector->data,512,global.debugfile);
  119.         }
  120.  
  121.         partition->bpb().get (partition->bootsector);
  122.  
  123.         printx ("\nBootsector:\n\n");
  124.         partition->print_bpb ();
  125.  
  126.         partition->info().get (partition->bpb());
  127.         if (global.debug_mode) partition->write_info_debugfile ();
  128.  
  129.         partition->check();
  130.  
  131.         fat16 fat1 (partition,1);
  132.         fat16 fat2 (partition,2);
  133.  
  134.         fat1.check_against (&fat2);
  135.  
  136.         dword new_part_min_sector = partition->info().start_data + (dword) 4085 * partition->bpb().sectors_per_cluster;
  137.         dword new_part_min_cylinder = (new_part_min_sector + partition->partition_info->start_sector_abs - 1) / (hd.geometry.heads * hd.geometry.sectors) + 1;
  138.  
  139.         if (new_part_min_cylinder > partition->partition_info->end_cylinder)
  140.             error ("Partition too small - can't split");
  141.  
  142.         dword min_free_cluster = fat1.min_cluster ();
  143.         dword min_free_sector = partition->info().start_data + (min_free_cluster - 2) * (dword) partition->bpb().sectors_per_cluster;
  144.         dword min_free_cylinder = (min_free_sector + partition->partition_info->start_sector_abs - 1) / (hd.geometry.heads * hd.geometry.sectors) + 1;
  145.  
  146.         if (min_free_cylinder > partition->partition_info->end_cylinder)
  147.             error ("Last Cylinder is not free");
  148.  
  149.         if (new_part_min_cylinder < min_free_cylinder) new_part_min_cylinder = min_free_cylinder;
  150.  
  151.         if (ask_if_save()) save_root_and_boot(&hd,partition);
  152.  
  153.         dword new_start_cylinder;
  154.         if (global.new_start_cylinder_cmdline)
  155.         {
  156.             new_start_cylinder = global.new_start_cylinder_cmdline;
  157.             if ((new_start_cylinder < new_part_min_cylinder) || (new_start_cylinder > partition->partition_info->end_cylinder))
  158.                 error ("Invalid new start cylinder: %lu",new_start_cylinder);
  159.         }
  160.         else new_start_cylinder = ask_for_new_start_cylinder (partition->partition_info->start_cylinder, new_part_min_cylinder,partition->partition_info->end_cylinder, hd.geometry.heads * hd.geometry.sectors);
  161.  
  162.         fat2.check_empty (new_start_cylinder * hd.geometry.heads * hd.geometry.sectors - partition->partition_info->start_sector_abs);
  163.  
  164.         hd.calculate_new_root (new_start_cylinder,partition);
  165.  
  166.         hd.partition_table().put (hd.rootsector);
  167.  
  168.         hd.partition_table().get (hd.rootsector);
  169.  
  170.         printx ("\nNew Partition Table:\n\n");
  171.         hd.print_partition_table ();
  172.  
  173.         hd.check();
  174.  
  175.         if (ask_if_continue ())
  176.         {
  177.             harddrive = hd;
  178.             break;
  179.         }
  180.     }
  181.  
  182.     partition->calculate_new_boot ();
  183.  
  184.     partition->bpb().put (partition->bootsector);
  185.  
  186.     partition->bpb().get (partition->bootsector);
  187.  
  188.     printx ("\nNew Bootsector:\n\n");
  189.     partition->print_bpb ();
  190.  
  191.     partition->info().get (partition->bpb());
  192.     if (global.debug_mode) partition->write_info_debugfile ();
  193.  
  194.     partition->check();
  195.  
  196.     if (!global.test_mode)
  197.     {
  198.         ask_for_write_permission();
  199.  
  200.         if (harddrive.rootsector->write())
  201.             error ("Error writing Root Sector");
  202.  
  203.         if (partition->bootsector->write ())
  204.             error ("Error writing Boot Sector");
  205.  
  206.         printx ("Repartitioning complete\n");
  207.     }
  208. }
  209.